﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApp
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Display();
            }
        }

        private void Display()
        {
            string sql = "select * from Note";
            GridView1.DataSource = DbHelper.GetData(sql);
            GridView1.DataBind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GridView1.DataKeys[e.RowIndex].Value.ToString();

            string sql = "delete Note where Noteid=" + id;

            var res=DbHelper.UpdateData(sql);

            if (res > 0)
            {
                Response.Write("<script>alert('删除成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('删除失败')</script>");
            }

            Display();
        }

        protected void selBtn_Click(object sender, EventArgs e)
        {
            var value = selTxB.Text.Trim();
            var sql = "select * from Note where Keys like '%" + value + "%' or Title like '%" + value + "%'";

            var table = DbHelper.GetData(sql);

            GridView1.DataSource = table;
            GridView1.DataBind();

        }

        protected void clBtn_Click(object sender, EventArgs e)
        {
            selTxB.Text = "";
            Display();
        }

        protected void addBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddNote.aspx");
        }
    }
}